﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.OleDb;
using Models;
using System.Web.Security;

namespace Lohus.Controllers
{
    public class ServiceController : Controller
    {
        //
        // GET: /Service/

        public ActionResult Index()
        {

            return View();
        }

        /// <summary>
        /// 获取服务领域文章列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            string sql = "select service.content,service.id,service.preview,c.Img,c.Sname,service.title from service left join servicecategories c on service.categoryid = c.id where service.id in (select max(service.id) from service group by service.categoryid)";
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                try
                {
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand(sql, con);
                    List<ServiceExt> list = new List<ServiceExt>();
                    using (OleDbDataReader dr = cmd.ExecuteReader())
                    {
                        if (dr != null)
                        {
                            while (dr.Read())
                            {
                                ServiceExt sv = new ServiceExt();
                                sv.Content = dr["content"].ToString();
                                sv.Img = dr["img"].ToString();
                                sv.Title = dr["Title"].ToString();
                                sv.ID = Convert.ToInt32(dr["ID"].ToString());
                                sv.Preview = dr["Preview"].ToString();
                                sv.SName = dr["Sname"].ToString();
                                list.Add(sv);
                            }
                        }
                    }
                    return Json(list, JsonRequestBehavior.AllowGet);
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 查看服务内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult PreviewService(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return View() ;
            }
            id = id.Trim().Replace("\'", "");
            string sql = "select service.*,serviceCategories.Img  from service left join serviceCategories on service.categoryID = serviceCategories.ID where service.id = " + id;
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                try
                {
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand(sql, con);
                    using (OleDbDataReader dr = cmd.ExecuteReader())
                    {
                        if (dr != null)
                        {
                            while (dr.Read())
                            {
                                ServiceExt sv = new ServiceExt();
                                sv.Content = dr["content"].ToString();
                                sv.Img = dr["img"].ToString();
                                sv.Title = dr["Title"].ToString();
                                sv.ID = Convert.ToInt32(dr["ID"].ToString());
                                sv.Preview = dr["Preview"].ToString();
                                ViewData["svContent"] = sv.Content;
                                ViewData["svImg"] = sv.Img;
                                ViewData["svTitle"] = sv.Title;
                                ViewData["svID"] = sv.ID;
                            }
                        }
                    }
                }
                catch
                {
                    return View();
                }
            }
            return View();
        }
        public ActionResult ShowList()
        {
            return View();
        }
        public ActionResult ServiceArticle(string ID)
        {
            if (FormsAuthentication.GetAuthCookie("user", false) == null)
            {
                throw new Exception("无访问权限！");
            }
            if (!string.IsNullOrEmpty(ID))
            {
                ID = ID.Trim('\'');
                string sql = "select service.*,serviceCategories.Img,serviceCategories.ID as SID  from service left join serviceCategories on service.categoryID = serviceCategories.ID where service.id = " + ID;
                using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
                {
                    try
                    {
                        con.Open();
                        OleDbCommand cmd = new OleDbCommand(sql, con);
                        using (OleDbDataReader dr = cmd.ExecuteReader())
                        {
                            if (dr != null)
                            {
                                while (dr.Read())
                                {
                                    ServiceExt sv = new ServiceExt();
                                    sv.Content = dr["content"].ToString();
                                    sv.Img = dr["img"].ToString();
                                    sv.Title = dr["Title"].ToString();
                                    sv.ID = Convert.ToInt32(dr["ID"].ToString());
                                    sv.Preview = dr["Preview"].ToString();
                                    ViewData["svContent"] = sv.Content;
                                    ViewData["svImg"] = sv.Img;
                                    ViewData["Preview"] = sv.Preview;
                                    ViewData["svTitle"] = sv.Title;
                                    ViewData["SID"] = dr["ID"].ToString();
                                    ViewData["svID"] = dr["SID"].ToString();
                                }
                            }
                        }
                    }
                    catch
                    {
                        return View();
                    }
                }
            }
            return View();
        }
        /// <summary>
        /// 删除案例文章
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ActionResult DeleteArticle(string ID)
        {

            if (FormsAuthentication.GetAuthCookie("user", false) == null)
            {
                throw new Exception("无访问权限！");
            }
            string sql = "delete from service where id =" + ID;
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                try
                {
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand(sql, con);
                    return Json(cmd.ExecuteNonQuery() > 0, JsonRequestBehavior.AllowGet);
                }
                catch
                {
                    return Json(false, JsonRequestBehavior.AllowGet);
                }
            }
        }
    }
}
